package org.ztest.thrift;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.ztest.thrift.echo.Echo;
import org.ztest.thrift.tutorial.Calculator;

// 阻塞型客户端
public class JavaMultiplexClient2
{
    public static void main(String[] args) throws Exception {
        TTransport transport = new TSocket("localhost", 9090);
        transport.open();

        int i = 10000;
        while (i-- > 0) {
            Echo.Client ec = new Echo.Client(getProtocol(transport));
            System.out.println(ec.echo("This is blocking client " + i));

            Calculator.Client cc = new Calculator.Client(getProtocol2(transport));
            System.out.println(cc.add(i, i));
        }

        transport.close();
    }

    public static void test1() {
        TTransport transport = new TSocket("localhost", 9090);
        TProtocol protocol = new TCompactProtocol(transport);
        TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "ECHO");
        Echo.Client ec = new Echo.Client(tp);
        try {
            ec.echo("This is an echo message");
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public static void test2() {
        TTransport transport = new TSocket("localhost", 9090);
        TProtocol protocol = new TCompactProtocol(transport);
        TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "CALCULATOR");
        Calculator.Client cc = new Calculator.Client(tp);
        try {
            cc.add(1, 2);
        } catch (TException e) {
            e.printStackTrace();
        }
    }

    public static void test3(RPCAction action) {
        TTransport transport = null;
        try {
            transport = new TSocket("localhost", 9090);
            TProtocol protocol = new TCompactProtocol(transport);
            TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "CALCULATOR");
            action.action(tp);
        } catch (Exception e) {

        } finally {
            if (transport != null && transport.isOpen()) {
                transport.close();
            }
        }
    }

    public static void test31() {
        RPCAction action = protocol -> {
            Echo.Client ec = new Echo.Client(protocol);
            ec.echo("Run in asynchronous mode");
        };
        test3(action);
    }

    static interface RPCAction
    {
        void action(TProtocol protocol) throws Exception;
    }

    public static TProtocol getProtocol(TTransport transport) {
        // 阻塞型客户端配合非阻塞型服务器使用时需要做如下转换
        transport = new TFramedTransport(transport);
        TProtocol protocol = new TCompactProtocol(transport);
        TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "ECHO");
        return tp;
    }

    public static TProtocol getProtocol2(TTransport transport) {
        // 阻塞型客户端配合非阻塞型服务器使用时需要做如下转换
        transport = new TFramedTransport(transport);
        TProtocol protocol = new TCompactProtocol(transport);
        TMultiplexedProtocol tp = new TMultiplexedProtocol(protocol, "CALCULATOR");
        return tp;
    }
}
